package com.baizhi.shane.sys.manager;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.sys.entity.SysUser;
import com.baizhi.shane.sys.service.SysUserService;
import com.baizhi.shane.sys.vo.LoginUserVO;
import com.baizhi.shane.sys.vo.UserLoginInVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class UserManager {
    @Resource
    private RedisManager redisManager;
    @Resource
    private HttpServletRequest request;
    @Resource
    private SysUserService sysUserService;

    /**
     * 获取当前登录用户信息
     *
     * @Author 闻柏智
     * @Date 17:31 2023/4/30
     * @Param
     * @return com.baizhi.baiweiexam.sys.entity.SysUser
     **/
    public SysUser getLoginUser() {
        String token = request.getHeader(SysMapping.TOKEN);
        Long userId = (Long) redisManager.get(token);
        LambdaQueryWrapper<SysUser> qwSu = new LambdaQueryWrapper<>();
        qwSu.select(SysUser::getId,SysUser::getUsername,SysUser::getRealname);
        qwSu.eq(SysUser::getId,userId);
        SysUser loginUser = sysUserService.getOne(qwSu);
        return loginUser;
    }



    /**
     * 获取当前登录用户ID
     *
     * @Author 闻柏智
     * @Date 20:03 2023/4/30
     * @Param
     * @return java.lang.Long
     **/
    public Long getLoginUserId() {
        String token = request.getHeader(SysMapping.TOKEN);
        Long userId = (Long) redisManager.get(token);
        return userId;
    }
}
